// eslint-disable-next-line import/no-unresolved
import FlowGraph from './index';

// eslint-disable-next-line import/prefer-default-export
export function checkSlide() {
  const { graph } = FlowGraph;
  const { cells } = graph.toJSON();
  const nodes = [];
  const edgesArr = [];
  // eslint-disable-next-line no-restricted-syntax
  for (const cellItem of cells) {
    if (cellItem.shape === 'edge') {
      edgesArr.push(cellItem);
    }
  }
  // 处理edge
  const edges = [];
  // eslint-disable-next-line no-restricted-syntax
  for (const edge of edgesArr) {
    const obj = {
      source: edge.source.cell,
      target: edge.target.cell,
    };
    edges.push(obj);
  }

  // 邻接矩阵
  const graphObj = {};
  // 结点个数和边的个数
  // 标记矩阵,0为当前结点未访问,1为访问过,-1表示当前结点后边的结点都被访问过。
  const visited = {};
  // 是否是DAG（有向无环图）
  let isDAG = true;

  function DFS(i) {
    // eslint-disable-next-line no-console
    console.log(`正在访问结点${nodes[i]}`);
    // 结点i变为访问过的状态
    // eslint-disable-next-line no-use-before-define
    visited[nodes[i]] = 1;
    // eslint-disable-next-line no-plusplus
    for (let j = 0; j < nodes.length; j++) {
      // 如果当前结点有指向的结点
      // eslint-disable-next-line no-use-before-define
      if (graphObj[nodes[i]][nodes[j]] !== 0) {
        // 并且已经被访问过
        if (visited[nodes[j]] === 1) {
          isDAG = false; // 有环
          break;
        } else if (visited[nodes[j]] === -1) {
          // 当前结点后边的结点都被访问过，直接跳至下一个结点
          // eslint-disable-next-line no-continue
          continue;
        } else {
          DFS(j); // 否则递归访问
        }
      }
    }
    // 遍历过所有相连的结点后，把本节点标记为-1
    visited[nodes[i]] = -1;
  }

  // 创建图,以邻接矩阵表示
  // eslint-disable-next-line no-shadow
  function create(nodes, edges) {
    for (let i = 0; i < nodes.length; i++) {
      const pre = nodes[i];
      graphObj[pre] = {};
      for (let j = 0; j < nodes.length; j++) {
        const next = nodes[j];
        graphObj[pre][next] = 0;
      }
    }
    for (let k = 0; k < edges.length; k++) {
      const edge = edges[k];
      // eslint-disable-next-line no-use-before-define
      graphObj[edge.source][edge.target] = 1;
    }
    // 初始化color数组为0，表示一开始所有顶点都未被访问过
    for (let i = 0; i < nodes.length; i++) {
      // eslint-disable-next-line no-use-before-define
      visited[nodes[i]] = 0;
    }
  }
  edges.forEach(e => {
    const { source, target } = e;
    if (!nodes.includes(source)) {
      nodes.push(source);
    }
    if (!nodes.includes(target)) {
      nodes.push(target);
    }
  });
  create(nodes, edges);
  // 保证每个节点都遍历到，排除有的结点没有边的情况
  // eslint-disable-next-line no-plusplus
  for (let i = 0; i < nodes.length; i++) {
    // 该结点后边的结点都被访问过了，跳过它
    if (visited[nodes[i]] === -1) {
      // eslint-disable-next-line no-continue
      continue;
    }
    DFS(i);
    if (!isDAG) {
      // eslint-disable-next-line no-console
      console.log('有环');
      return true;
    }
  }
  if (isDAG) {
    // eslint-disable-next-line no-console
    console.log('无环');
    return false;
  }
}
